package com.fuluwa.application.pay.biz.impl.tenpay;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.fuluwa.application.FlwBizException;
import com.fuluwa.application.pay.biz.PayBiz;
import com.fuluwa.application.pay.bo.TenpayBo;
import com.fuluwa.application.pay.util.ParametersUtil;
import com.fuluwa.infra.utils.HttpUtil;
import com.fuluwa.infra.utils.MD5Util;
@Service("tenpayBiz")
public class TenpayBizImpl implements PayBiz<TenpayBo> {

	private static final String tenpayPayGate = "https://gw.tenpay.com/gateway/pay.htm";
	private static final String key = "8934e7d15453e97507ef794cf7b0519d";
	private static final String CHARSET_UTF8 = "UTF-8";

	@Override
	public String genPayUrl(TenpayBo payBo) throws FlwBizException {
		String payUrl = "";

		try {
			// 1. 生成params字符串
			Map<String, String> sortedMap = ParametersUtil.coverInputObjToMap(payBo, true);

			// 2. 生成MD5签名,注意Map的排序，过滤掉空字符串等，最后转成大写
			String sign = MD5Util.md5Sign(sortedMap, key, CHARSET_UTF8).toUpperCase();

			// 3. 拼接URL
			sortedMap.put("sign", sign);
			String queryString = HttpUtil.toQueryString(sortedMap, CHARSET_UTF8);
			payUrl = tenpayPayGate + "?" + queryString;
		} catch (FlwBizException e) {
			// LOG exception
			throw e;
		} catch (Exception e) {
			// TODO 封装成业务异常 LOG exception
		}

		return payUrl;
	}

}
